﻿/**
* @author Alexander Rindo
* @project Flashview
* @version 0.4
* @purpose Class for managing the Thumbnails
**/

package com.alexrindo.flashview.UI.thumbnails 
{
	import com.alexrindo.flashview.data.GalleryData;
	import com.alexrindo.flashview.image.ImageManager;
	import com.alexrindo.flashview.layout.LayoutManager;
	import com.greensock.TweenLite;
	import com.greensock.easing.*;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class ThumbnailHolder extends MovieClip
	{
		public static const THUMBS_LOADED:String = "thumbs_loaded"
		public static const POSITION_CHANGE:String = "position_change"
		
		private var _layout:LayoutManager
		private var _data:GalleryData
		private var _images:ImageManager
		private var _content:Sprite
		private var _thumbContainer:Array
		private var _thumbCount:int
		
		public function ThumbnailHolder() 
		{
			_data = GalleryData.getInstance()
			positionBackground()
		}
		
		private function positionBackground():void
		{
			_layout = LayoutManager.getInstance(stage)
			_layout.addResizeObject(thumbBackground,LayoutManager.WIDTH_RESIZE)
		}
		
		public function addThumbnails():void
		{
			_thumbContainer = new Array()
			_content = new Sprite()
			
			for (var i:uint = 0; i < _data.pictureCount; i++) 
			{	
				_thumbContainer[i] = new Thumbnail(_data.thumbnailFolderURL + _data.getImageURL(i), i)
				_thumbContainer[i].addEventListener(Event.COMPLETE, onThumbnailComplete)
				_thumbContainer[i].x = (120*i) + 30
				_thumbContainer[i].y = 22
				_content.addChild(_thumbContainer[i])
			}
			addChild(_content)
		}
		
		private function onThumbnailComplete(e:Event):void
		{
			_thumbCount++
			if (_thumbCount == _data.pictureCount)
			{
				dispatchEvent(new Event(THUMBS_LOADED))
				_images = ImageManager.getInstance()
				addListeners()
			}
		}
		
		private function addListeners():void
		{
			addEventListener(MouseEvent.MOUSE_WHEEL, scrollbar.ThumbScrollerWheel)
			hideThumbHolder.addEventListener(MouseEvent.CLICK, hideThumbnailHolder)
		}
		
		private function hideThumbnailHolder(e:MouseEvent):void 
		{
			if (hideThumbHolder.hide_mc.alpha != 0)
			{
				TweenLite.to(hideThumbHolder.hide_mc, 0.2, {alpha: 0, ease:Circ.easeOut});
				TweenLite.to(hideThumbHolder.show_mc, 0.2, {alpha: 1, ease:Circ.easeOut});
				TweenLite.to(this, 0.3, { y: -115, ease:Quint.easeOut } );
				
				_layout.imageOffset = 0
				_layout.setLayout()
				_images.drawFrame()
			}
			
			else
			{
				TweenLite.to(hideThumbHolder.hide_mc, 0.2, {alpha: 1, ease:Circ.easeOut});
				TweenLite.to(hideThumbHolder.show_mc, 0.2, {alpha: 0, ease:Circ.easeOut});
				TweenLite.to(this, 0.3, { y: 0, ease:Quint.easeOut } );
				
				_layout.imageOffset = 55
				_layout.setLayout()
				_images.drawFrame()
			}
			dispatchEvent(new Event(POSITION_CHANGE))
		}
		
		public function get content():Sprite
		{
			return _content
		}
	}
}